import base64
from typing import Optional
from unittest.mock import MagicMock, patch

import pytest

from posthog.models.dashboard import Dashboard
from posthog.models.exported_asset import ExportedAsset
from posthog.tasks import exporter
from posthog.tasks.exports.image_exporter import get_driver
from posthog.test.base import APIBaseTest


class MockWebDriver(MagicMock):
    def find_element_by_css_selector(self, name: str) -> Optional[MagicMock]:
        return MagicMock()  # Always return something for wait_for_css_selector

    def find_element_by_class_name(self, name: str) -> Optional[MagicMock]:
        return None  # Never return anything for Spinner


@patch("posthog.tasks.exports.image_exporter.uuid")
class TestExporterTask(APIBaseTest):
    exported_asset: ExportedAsset = None  # type: ignore

    @classmethod
    def setUpTestData(cls) -> None:
        super().setUpTestData()
        dashboard = Dashboard.objects.create(team=cls.team, name="example dashboard", created_by=cls.user)
        cls.exported_asset = ExportedAsset.objects.create(
            team=cls.team, dashboard_id=dashboard.id, export_format="image/png"
        )

        example_png = b"iVBORw0KGgoAAAANSUhEUgAAAB4AAAATCAYAAACHrr18AAAAAXNSR0IArs4c6QAAAJhlWElmTU0AKgAAAAgABQESAAMAAAABAAEAAAEaAAUAAAABAAAASgEbAAUAAAABAAAAUgExAAIAAAAHAAAAWodpAAQAAAABAAAAYgAAAAAAAABIAAAAAQAAAEgAAAABR29vZ2xlAAAABJAAAAcAAAAEMDIyMKABAAMAAAABAAEAAKACAAQAAAABAAAAHqADAAQAAAABAAAAEwAAAAD9Mn6HAAAACXBIWXMAAAsTAAALEwEAmpwYAAADLWlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iWE1QIENvcmUgNi4wLjAiPgogICA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPgogICAgICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIgogICAgICAgICAgICB4bWxuczp4bXA9Imh0dHA6Ly9ucy5h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Q1GvMhu9VoZEY0zJqRlA+pxEVF29hPlL/4y1qw/j1DlgTfFb6E4w0dLXjPsXzsXRjZsI7sHUVS8h5J6OUGAAKfFB9FUdA7wX4EgajzAXVvknd8l9ePH557GTnquiHLUyI7eYJnbYNRdwZUjkJ6p61oNnJSmnThY9KuKO+0Rayn0iVdVS98giacpPE1m7WI6kGyInqOrgHumrWCKho/NEvl8ozfuXinSflJPfVYgVWoZXEavqp5/tGFF1hMpWQr+ubL2aOvEwXx/6ai6TfgNqSfvchfl4+4X3gEthZLxagq7xk+HtuYJFWVNQf3wvrW3G2Nyn4LdlIBAagssVQGiwG3bDpjI9Nr68AccrK7FmzSoUrVuHnZ/tINXsGBE7X6jqMWOAJ5/4J2pJb0XZCmacPvT46vHw4jx8/detQGsPsjZtRsDlRE37ZUxOdmLwwv8AnxcTsxcj5poEv+lk2mTS0JgmyWvBypXIy8/Hgvk5Fs9F64pQsu3f8Pt5qqmUb1JcAVpx64z7UFBwCFdqCf7lHUhxeDDceRHLs2bjP+ueBJouI/3tDzBwy1R0hXrhnhBG71keHr2NcM9fhWHXHAQZO80IqS2KXbu2Y/fnO1F+sBzxcW5kZmbjlY0b8ZeClSguLobHUw/t8I8iD6/Yhjkzc1FXewZ7vilCbipQSS8TWs9i6rgAqluqUbiN1M9MxC9Hi5Genmh52Bt2YnpmAcSdhpCWiB9O1+CBZYWWmJQBSlQGj78wz2fDxl8H6wcjhgMHyqBd9or09TK/U46KhmDIRM5snqfXeBp1NAB2dtgEweEgXHfO4CHk40l1DSD1MPk3EmRck9I4JhG9nQPMK11wuuKJSoWbJs9dg8dtxPp3cTjsimckJyfj/yauOPzsM+9nAAAAAElFTkSuQmCC"

        with open("/tmp/posthog_test_exporter.png", "wb") as fh:
            fh.write(base64.decodebytes(example_png))

    @patch("posthog.tasks.exports.image_exporter.get_driver")
    def test_exporter_runs(self, mock_get_driver: MagicMock, mock_uuid: MagicMock) -> None:
        mock_uuid.uuid4.return_value = "posthog_test_exporter"
        mock_get_driver.return_value = MockWebDriver()

        assert self.exported_asset.content is None
        assert self.exported_asset.content_location is None

        exporter.export_asset(self.exported_asset.id)
        self.exported_asset.refresh_from_db()

        assert self.exported_asset.content is None
        assert self.exported_asset.content_location is not None

    @pytest.mark.skip("Currently broken due to an issue with ChromeDriver")
    def test_exporter_setsup_selenium(self, mock_uuid: MagicMock) -> None:
        driver = get_driver()

        assert driver is not None

        driver.get("https://example.com")

        if driver:
            driver.close()
